import Vue from 'vue'
import Router from 'vue-router'
import routes from './routes'
import store from '../store'
import {Toast} from 'wdui'

Vue.use(Router)

const router = new Router({
    // mode: 'history',
    // scrollBehavior: () => ({ y: 0 }),
    routes
})

router.beforeEach((to, from, next) => {
    if (to.matched.some(record => record.meta.requiresAuth)) {
        const auth = localStorage.user
        if (!auth) {
            Toast({
              content: '请先登录'
            })
            setTimeout(() => {
                next({
                    path: '/login',
                    // query: { redirect: to.fullPath }
                })
            }, 500)
        } else {
            let distance = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop
            store.commit('saveTopData', distance)
            next()
        }
    } else {
        next() // 确保一定要调用 next()
    }
})


router.afterEach(()=>{
    let top = store.state.top_distance
    // console.log(top)
})

export default router
